﻿Public Class ucEditProfileMember
    Inherits System.Web.UI.UserControl

	Public Event Alert(ByVal msg As String)

	Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            If GlobalApp.DataSourceEditOfficer IsNot Nothing Then

                LoadProvince1()
                LoadProvince2()

                LoadDistinct1()
                LoadDistinct2()

                LoadSubDistinct1()
                LoadSubDistinct2()

                LoadPostCode1()
                LoadPostCode2()

                LoadBank()

                LoadStatus()

                LoadData()

                SetAutorizeEntity()
            End If
        End If
		InitJS()

		OFFICER_APP_ID.Attributes.Add("onBlur", "ChangeFormat(this,0,'.','',false,'text');ChangFormatID();")
		btnFalk.Attributes.Add("Style", "display:none")

		If HttpContext.Current.User.IsInRole("member") Then
			Me.trPassword.Visible = False
			Me.trRePassword.Visible = False
			Me.trRemark.Visible = False
			Me.trStatus.Visible = False
		End If
	End Sub

	Private Sub SetAutorizeEntity()
		Dim lsEntity As List(Of Entity.AuthorizationEntityItem)
		lsEntity = BL.Authentication.AutorizeEditOfficer(GlobalApp.Authen.role_id)
		If lsEntity IsNot Nothing Then
			For Each item As Entity.AuthorizationEntityItem In lsEntity

				Dim ctl As WebControl = Me.FindControl(item.entity_id)
				If ctl IsNot Nothing Then
					If item.IsHideControl Then
						ctl.Visible = False
					Else
						ctl.Enabled = False
					End If
				End If
			Next
		End If
	End Sub

	Private Sub LoadStatus()
		Me.ddlStatus.DataSource = BL.OfficerStatus.AllStatus
		Me.ddlStatus.DataTextField = "STATUS_DESC"
		Me.ddlStatus.DataValueField = "STATUS_ID"
		Me.ddlStatus.DataBind()
	End Sub

	Private Sub LoadBank()
		Me.BANK_ID.DataTextField = "BANK_NAME"
		Me.BANK_ID.DataValueField = "BANK_ID"
		Me.BANK_ID.DataSource = BL.Bank.GetAll
		Me.BANK_ID.DataBind()
		Me.BANK_ID.Items.Insert(0, New ListItem("--กรุณาระบุชื่อธนาคาร--", "0"))
	End Sub

	Private Sub InitJS()
		Dim sbJS As New Text.StringBuilder
		sbJS.AppendLine("var brithdayID = '" & Me.OFFICER_BIRTHDATE.ClientID & "';")
		sbJS.AppendLine("var carddayID = '" & Me.OFFICER_CARD_EXPIREDATE.ClientID & "';")
		sbJS.AppendLine("var appID = '" & Me.OFFICER_APP_ID.ClientID & "';")
        sbJS.AppendLine("var cardID = '" & Me.OFFICER_CARD_NO.ClientID & "';")
        sbJS.AppendLine("var cardTypeDesc = '" & Me.OFFICER_CARD_TYPE_DESC.ClientID & "';")
		Me.ltrJS.Text = "<script language=""JavaScript"">" & sbJS.ToString & "</script>"
	End Sub


	Private Sub LoadProvince1()
		OFFICER_CARD_ADDRESS_PROVINCE.DataTextField = "PROVINCE_NAME"
		OFFICER_CARD_ADDRESS_PROVINCE.DataValueField = "PROVINCE_ID"
		OFFICER_CARD_ADDRESS_PROVINCE.DataSource = BL.Province.GetAll
		OFFICER_CARD_ADDRESS_PROVINCE.DataBind()
		OFFICER_CARD_ADDRESS_PROVINCE.Items.Insert(0, New ListItem("--กรุณาระบุจังหวัด--", "0"))
	End Sub

	Private Sub LoadProvince2()
		OFFICER_MAIL_ADDRESS_PROVINCE.DataTextField = "PROVINCE_NAME"
		OFFICER_MAIL_ADDRESS_PROVINCE.DataValueField = "PROVINCE_ID"
		OFFICER_MAIL_ADDRESS_PROVINCE.DataSource = BL.Province.GetAll
		OFFICER_MAIL_ADDRESS_PROVINCE.DataBind()
		OFFICER_MAIL_ADDRESS_PROVINCE.Items.Insert(0, New ListItem("--กรุณาระบุจังหวัด--", "0"))
	End Sub

	Private Sub LoadDistinct1()
		If Not Me.OFFICER_CARD_ADDRESS_PROVINCE.SelectedValue.Equals("0") Then
			OFFICER_CARD_ADDRESS_DISTRICT.DataTextField = "DISTRICT_NAME"
			OFFICER_CARD_ADDRESS_DISTRICT.DataValueField = "DISTRICT_ID"
			OFFICER_CARD_ADDRESS_DISTRICT.DataSource = BL.District.GetAll(Me.OFFICER_CARD_ADDRESS_PROVINCE.SelectedValue)
			OFFICER_CARD_ADDRESS_DISTRICT.DataBind()
		Else
			OFFICER_CARD_ADDRESS_DISTRICT.Items.Clear()
		End If
		OFFICER_CARD_ADDRESS_DISTRICT.Items.Insert(0, New ListItem("--กรุณาระบุเขต/อำเภอ--", "0"))
	End Sub

	Private Sub LoadDistinct2()
		If Not Me.OFFICER_MAIL_ADDRESS_PROVINCE.SelectedValue.Equals("0") Then
			OFFICER_MAIL_ADDRESS_DISTRICT.DataTextField = "DISTRICT_NAME"
			OFFICER_MAIL_ADDRESS_DISTRICT.DataValueField = "DISTRICT_ID"
			OFFICER_MAIL_ADDRESS_DISTRICT.DataSource = BL.District.GetAll(Me.OFFICER_MAIL_ADDRESS_PROVINCE.SelectedValue)
			OFFICER_MAIL_ADDRESS_DISTRICT.DataBind()
		Else
			OFFICER_MAIL_ADDRESS_DISTRICT.Items.Clear()
		End If
		OFFICER_MAIL_ADDRESS_DISTRICT.Items.Insert(0, New ListItem("--กรุณาระบุเขต/อำเภอ--", "0"))
	End Sub

	Private Sub LoadSubDistinct1()
		If Not Me.OFFICER_CARD_ADDRESS_DISTRICT.SelectedValue.Equals("0") Then
			officer_card_address_subICT.DataTextField = "SUB_DISTRICT_NAME"
			officer_card_address_subICT.DataValueField = "SUB_DISTRICT_ID"
			officer_card_address_subICT.DataSource = BL.SubDistrict.AllSubDistrict(Me.OFFICER_CARD_ADDRESS_PROVINCE.SelectedValue, Me.OFFICER_CARD_ADDRESS_DISTRICT.SelectedValue)
			officer_card_address_subICT.DataBind()
		Else
			officer_card_address_subICT.Items.Clear()
		End If
		officer_card_address_subICT.Items.Insert(0, New ListItem("--กรุณาระบุแขวง/ตำบล--", "0"))
	End Sub

	Private Sub LoadSubDistinct2()
		If Not Me.OFFICER_MAIL_ADDRESS_DISTRICT.SelectedValue.Equals("0") Then
			OFFICER_MAIL_ADDRESS_SUB_DISTRICT.DataTextField = "SUB_DISTRICT_NAME"
			OFFICER_MAIL_ADDRESS_SUB_DISTRICT.DataValueField = "SUB_DISTRICT_ID"
			OFFICER_MAIL_ADDRESS_SUB_DISTRICT.DataSource = BL.SubDistrict.AllSubDistrict(Me.OFFICER_MAIL_ADDRESS_PROVINCE.SelectedValue, Me.OFFICER_MAIL_ADDRESS_DISTRICT.SelectedValue)
			OFFICER_MAIL_ADDRESS_SUB_DISTRICT.DataBind()
		Else
			OFFICER_MAIL_ADDRESS_SUB_DISTRICT.Items.Clear()
		End If
		OFFICER_MAIL_ADDRESS_SUB_DISTRICT.Items.Insert(0, New ListItem("--กรุณาระบุแขวง/ตำบล--", "0"))
	End Sub

	Private Sub LoadPostCode1()
		If Not Me.officer_card_address_subICT.SelectedValue.Equals("0") Then
			OFFICER_CARD_ADDRESS_POSTCODE.DataTextField = "POSTCODE"
			OFFICER_CARD_ADDRESS_POSTCODE.DataValueField = "POSTCODE"
			OFFICER_CARD_ADDRESS_POSTCODE.DataSource = BL.PostCode.AllPostCode(Me.OFFICER_CARD_ADDRESS_PROVINCE.SelectedValue, Me.OFFICER_CARD_ADDRESS_DISTRICT.SelectedValue)
			OFFICER_CARD_ADDRESS_POSTCODE.DataBind()
		Else
			OFFICER_CARD_ADDRESS_POSTCODE.Items.Clear()
		End If
		OFFICER_CARD_ADDRESS_POSTCODE.Items.Insert(0, New ListItem("--รหัสไปรษณีย์--", "0"))
	End Sub

	Private Sub LoadPostCode2()
		If Not Me.OFFICER_MAIL_ADDRESS_SUB_DISTRICT.SelectedValue.Equals("0") Then
			OFFICER_MAIL_ADDRESS_POSTCODE.DataTextField = "POSTCODE"
			OFFICER_MAIL_ADDRESS_POSTCODE.DataValueField = "POSTCODE"
			OFFICER_MAIL_ADDRESS_POSTCODE.DataSource = BL.PostCode.AllPostCode(Me.OFFICER_MAIL_ADDRESS_PROVINCE.SelectedValue, Me.OFFICER_MAIL_ADDRESS_DISTRICT.SelectedValue)
			OFFICER_MAIL_ADDRESS_POSTCODE.DataBind()
		Else
			OFFICER_MAIL_ADDRESS_POSTCODE.Items.Clear()
		End If
		OFFICER_MAIL_ADDRESS_POSTCODE.Items.Insert(0, New ListItem("--รหัสไปรษณีย์--", "0"))
	End Sub

	Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCancel.Click
		If HttpContext.Current.User.IsInRole("admin") Then
			Response.Redirect("OfficerManagement.aspx", False)
		Else
			Response.Redirect("ViewStatusRegister.aspx", False)
		End If
	End Sub

	Private Sub LoadData()

		Dim registerInfo As Entity.OfficerInfoItem
		registerInfo = GlobalApp.DataSourceEditOfficer
		With registerInfo

			'############################### Application 1 ##############################################
			Try
				Me.OFFICER_APP_ID.Text = Common.Utility.FormatAppId(.officer_app_id)
			Catch
				Me.OFFICER_APP_ID.Text = .officer_app_id
			End Try

			Me.OFFCIER_APP_TEL.Text = .offcier_app_tel
			Me.OFFICER_FIRSTNAME_THA.Text = .officer_firstname_tha
			Me.OFFICER_LASTNAME_THA.Text = .officer_lastname_tha
			Me.OFFICER_FIRSTNAME_ENG.Text = .officer_firstname_eng
			Me.OFFICER_LASTNAME_ENG.Text = .officer_lastname_eng
            Common.Utility.SelectChoice(Me.OFFICER_SEX, .officer_sex)
            Me.ddlCardType.SelectedValue = .officer_card_type
            Me.OFFICER_CARD_TYPE_DESC.Text = .officer_card_type_desc
            Me.OFFICER_CARD_NO.Text = .officer_card_no

            Try
                If Not IsNothing(.officer_birthdate) Then Me.OFFICER_BIRTHDATE.Text = .officer_birthdate.Value.AddYears(543).ToString("dd/MM/yyyy", Common.Utility.CulInfo)
            Catch
            End Try

			Try
				Me.OFFICER_CARD_EXPIREDATE.Text = .officer_card_expiredate.Value.AddYears(543).ToString("dd/MM/yyyy", Common.Utility.CulInfo)
			Catch
			End Try
			Common.Utility.SelectChoice(Me.OFFICER_CARD_EXPIREDATE_TYPE, .officer_card_expiredate_type)
			Common.Utility.SelectChoice(Me.OFFICER_MARRIED_STATUS, .officer_married_status)
			Common.Utility.SelectChoice(Me.ddlStatus, .officer_status)
			Me.txtStatus.Text = Common.Utility.ConvertStatus(.officer_status)
			Me.hdfStatusID.Value = .officer_status 'เก็บค่าไว้ตรวจเช็คตอน Save
			Me.OFFICER_REMARK.Text = .officer_remark

			Select Case .officer_status.ToUpper
				Case "WT"
					Me.chbChangeStatus.Text = "ตรวจสอบเอกสาร"
					Me.chbChangeStatus.Visible = True
					Me.chbChangeStatus1.Visible = False
					Me.chbChangeStatus2.Visible = False
				Case "WC"
					Me.chbChangeStatus.Text = "ยกเลิกการระงับ"
					Me.chbChangeStatus.Visible = True
					Me.chbChangeStatus1.Visible = False
					Me.chbChangeStatus2.Visible = False
				Case "WN"
					Me.chbChangeStatus.Text = "ต่ออายุ"
					Me.chbChangeStatus1.Text = "ระงับชั่วคราว"
					Me.chbChangeStatus2.Text = "โมฆะ"

					Me.chbChangeStatus.Visible = True
					Me.chbChangeStatus1.Visible = True
					Me.chbChangeStatus2.Visible = True
				Case "CE"
					Me.chbChangeStatus.Text = "ยกเลิกโมฆะ"
					Me.chbChangeStatus.Visible = True
					Me.chbChangeStatus1.Visible = False
					Me.chbChangeStatus2.Visible = False

				Case "EP"
					Me.chbChangeStatus.Text = "ต่ออายุ"
					Me.chbChangeStatus.Visible = True
					Me.chbChangeStatus1.Visible = False
					Me.chbChangeStatus2.Visible = False

				Case Else
					Me.chbChangeStatus.Text = "โมฆะ"
					Me.chbChangeStatus1.Text = "ระงับชั่วคราว"

					Me.chbChangeStatus.Visible = True
					Me.chbChangeStatus1.Visible = True
					Me.chbChangeStatus2.Visible = False
			End Select
			'############################### Application 1 ##############################################

			'############################### Application 2 ##############################################
			Me.OFFICER_CARD_ADDRESS_NO.Text = .officer_card_address_no
			Me.OFFICER_CARD_ADDRESS_MOO.Text = .officer_card_address_moo
			Me.OFFICER_CARD_ADDRESS_BUILDING.Text = .officer_card_address_building
			Me.OFFICER_CARD_ADDRESS_SOI.Text = .officer_card_address_soi
			Me.OFFICER_CARD_ADDRESS_STREET.Text = .officer_card_address_street

			Common.Utility.SelectChoiceByText(OFFICER_CARD_ADDRESS_PROVINCE, .officer_card_address_province)
			Me.LoadDistinct1()
			Common.Utility.SelectChoiceByText(OFFICER_CARD_ADDRESS_DISTRICT, .officer_card_address_district)
			Me.LoadSubDistinct1()
			Common.Utility.SelectChoiceByText(officer_card_address_subICT, .officer_card_address_sub)
			Me.LoadPostCode1()
			Common.Utility.SelectChoiceByText(OFFICER_CARD_ADDRESS_POSTCODE, .officer_card_address_postcode)

			Me.OFFICER_MAIL_ADDRESS_NO.Text = .officer_mail_address_no
			Me.OFFICER_MAIL_ADDRESS_MOO.Text = .officer_mail_address_moo
			Me.OFFICER_MAIL_ADDRESS_BUILDING.Text = .officer_mail_address_building
			Me.OFFICER_MAIL_ADDRESS_SOI.Text = .officer_mail_address_soi
			Me.OFFICER_MAIL_ADDRESS_STREET.Text = .officer_mail_address_street

			Common.Utility.SelectChoiceByText(OFFICER_MAIL_ADDRESS_PROVINCE, .officer_mail_address_province)
			Me.LoadDistinct2()
			Common.Utility.SelectChoiceByText(OFFICER_MAIL_ADDRESS_DISTRICT, .officer_mail_address_district)
			Me.LoadSubDistinct2()
			Common.Utility.SelectChoiceByText(OFFICER_MAIL_ADDRESS_SUB_DISTRICT, .officer_mail_address_sub)
			Me.LoadPostCode2()
			Common.Utility.SelectChoiceByText(OFFICER_MAIL_ADDRESS_POSTCODE, .officer_mail_address_postcode)

			Me.OFFICER_MAIL_PHONE.Text = .officer_mail_phone
			Me.OFFICER_MAIL_EMAIL.Text = .officer_mail_email
			'############################### Application 2 ##############################################

			'############################### Application 3 ##############################################
			Me.hfSponserID.Value = .officer_sponsor
			Me.OFFICER_SPONSOR.Text = .officer_sponsor_appid
			Me.OFFICER_SPONSOR_NAME.Text = .officer_sponsor_name
			Me.OFFICER_SPONSOR_SURENAME.Text = .officer_sponsor_surename
			Me.OFFICER_SPONSOR_TEL.Text = .officer_sponsor_tel

			Me.hfSenderID.Value = .officer_sender
			Me.OFFICER_SENDER.Text = .officer_sender_appid
			Me.OFFICER_SENDER_NAME.Text = .officer_sender_name
			Me.OFFICER_SENDER_SURENAME.Text = .officer_sender_surename
			Me.OFFICER_SENDER_TEL.Text = .officer_sender_tel

			Me.hfUplineID.Value = .officer_upline
			Me.OFFICER_UPLINE.Text = .officer_upline_appid
			Me.OFFICER_UPLINE_NAME.Text = .officer_upline_name
			Me.OFFICER_UPLINE_SURENAME.Text = .officer_upline_surename
			Me.OFFICER_UPLINE_TEL.Text = .officer_upline_tel
			Me.OFFICER_UPLINE_EMPTYSIDE.Text = .officer_upline_emptyside

			If Not String.IsNullOrEmpty(OFFICER_UPLINE_EMPTYSIDE.Text) Then
				If OFFICER_UPLINE_EMPTYSIDE.Text.Equals("L") Then
					OFFICER_UPLINE_EMPTYSIDE.Text = "ซ้าย"
				ElseIf OFFICER_UPLINE_EMPTYSIDE.Text.Equals("R") Then
					OFFICER_UPLINE_EMPTYSIDE.Text = "ขวา"
				End If
			End If
			'############################### Application 3 ##############################################

			'############################### Application 4 ##############################################
			Me.OFFICER_BANK_ACCOUNT_NAME.Text = .officer_bank_account_name
			Common.Utility.SelectChoice(Me.BANK_ID, .bank_id)
			Me.OFFICER_BANK_BRANCH.Text = .officer_bank_branch
			Me.OFFICER_BANK_ACCOUNT_CODE.Text = .officer_bank_account_code
			'############################### Application 4 ##############################################

		End With
	End Sub

	Public Shared Function ConvertSide(ByVal side As String) As String
		If side.ToUpper.Equals("L") Then
			Return "ซ้าย"
		Else
			Return "ขวา"
		End If
	End Function

	Protected Sub btnSponsorSearch_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSponsorSearch.Click
		If HttpContext.Current.User.IsInRole("admin") Then
			Me.gvOffical.DataSource = BL.Officer.SearchSponser_Staff(Me.txtSearchSponsor.Text)
		Else
			Me.gvOffical.DataSource = BL.Officer.SearchSponser_Member(Me.txtSearchSponsor.Text, GlobalApp.Authen.offcier_id)
		End If
		Me.gvOffical.PageIndex = 0
		Me.gvOffical.DataBind()
		Me.mpu.Show()
		Me.Session("sponsorsearch") = Me.gvOffical.DataSource
	End Sub

	Protected Sub gvOffical_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvOffical.RowCommand
		If e.CommandName.Equals("Select") Then
			Dim gvRow As GridViewRow = gvOffical.Rows(e.CommandArgument)
			Dim oj As LinkButton = gvRow.Cells(0).Controls(0)
			OFFICER_SPONSOR.Text = oj.Text
			OFFICER_SPONSOR_NAME.Text = gvRow.Cells(1).Text
			OFFICER_SPONSOR_SURENAME.Text = gvRow.Cells(2).Text
			OFFICER_SPONSOR_TEL.Text = gvRow.Cells(3).Text

			Dim hf As HiddenField
			hf = gvRow.Cells(4).FindControl("HiddenField1")
			Me.hfSponserID.Value = hf.Value

			Me.mpu.Hide()
		End If
	End Sub

	Protected Sub btnUplineSearch_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnUplineSearch.Click
		Me.gvOfficalUpline.DataSource = BL.Officer.SearchUpline(Me.txtSearchUpline.Text, Me.hfSponserID.Value)
		Me.gvOfficalUpline.PageIndex = 0
		Me.gvOfficalUpline.DataBind()
		Me.mpuUpline.Show()
		Me.Session("uplinesearch") = Me.gvOfficalUpline.DataSource
	End Sub

	Protected Sub gvOfficalUpline_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvOfficalUpline.RowCommand
		If e.CommandName.Equals("Select") Then
			Dim gvRow As GridViewRow = gvOfficalUpline.Rows(e.CommandArgument)
			Dim oj As LinkButton = gvRow.Cells(0).Controls(0)
			OFFICER_UPLINE.Text = oj.Text
			OFFICER_UPLINE_NAME.Text = gvRow.Cells(1).Text
			OFFICER_UPLINE_SURENAME.Text = gvRow.Cells(2).Text
			OFFICER_UPLINE_TEL.Text = gvRow.Cells(3).Text
			Try
				OFFICER_UPLINE_EMPTYSIDE.Text = DirectCast(gvRow.Cells(4).FindControl("Label1"), Label).Text
			Catch ex As Exception
				Common.LogError.AddLogException(ex)
			End Try
			Dim hf As HiddenField
			hf = gvRow.Cells(5).FindControl("HiddenField1")
			Me.hfUplineID.Value = hf.Value
			Me.mpuUpline.Hide()
		End If
	End Sub

	Protected Sub btnSenderSearch_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSenderSearch.Click
		Me.gvOfficalSender.DataSource = BL.Officer.SearchSender(Me.txtSearchSender.Text, Me.OFFICER_SPONSOR.Text)
		Me.gvOfficalSender.PageIndex = 0
		Me.gvOfficalSender.DataBind()
		Me.mpuSender.Show()
		Me.Session("sendersearch") = Me.gvOfficalSender.DataSource
	End Sub

	Protected Sub gvOfficalSender_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvOfficalSender.RowCommand
		If e.CommandName.Equals("Select") Then
			Dim gvRow As GridViewRow = gvOfficalSender.Rows(e.CommandArgument)
			Dim oj As LinkButton = gvRow.Cells(0).Controls(0)
			OFFICER_SENDER.Text = oj.Text
			OFFICER_SENDER_NAME.Text = gvRow.Cells(1).Text
			OFFICER_SENDER_SURENAME.Text = gvRow.Cells(2).Text
			OFFICER_SENDER_TEL.Text = gvRow.Cells(3).Text
			Dim hf As HiddenField
			hf = gvRow.Cells(4).FindControl("HiddenField1")
			Me.hfSenderID.Value = hf.Value
			Me.mpuSender.Hide()
		End If
	End Sub

	Protected Sub btnCopy_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCopy.Click
		Me.OFFICER_MAIL_ADDRESS_NO.Text = Me.OFFICER_CARD_ADDRESS_NO.Text
		Me.OFFICER_MAIL_ADDRESS_BUILDING.Text = Me.OFFICER_CARD_ADDRESS_BUILDING.Text
		Me.OFFICER_MAIL_ADDRESS_MOO.Text = Me.OFFICER_CARD_ADDRESS_MOO.Text
		Me.OFFICER_MAIL_ADDRESS_BUILDING.Text = Me.OFFICER_CARD_ADDRESS_BUILDING.Text
		Me.OFFICER_MAIL_ADDRESS_SOI.Text = Me.OFFICER_CARD_ADDRESS_SOI.Text
		Me.OFFICER_MAIL_ADDRESS_STREET.Text = Me.OFFICER_CARD_ADDRESS_STREET.Text
		Common.Utility.SelectChoice(OFFICER_MAIL_ADDRESS_PROVINCE, OFFICER_CARD_ADDRESS_PROVINCE.SelectedValue)
		Me.LoadDistinct2()
		Common.Utility.SelectChoice(OFFICER_MAIL_ADDRESS_DISTRICT, OFFICER_CARD_ADDRESS_DISTRICT.SelectedValue)
		Me.LoadSubDistinct2()
		Common.Utility.SelectChoice(OFFICER_MAIL_ADDRESS_SUB_DISTRICT, officer_card_address_subICT.SelectedValue)
		Me.LoadPostCode2()
		Common.Utility.SelectChoice(OFFICER_MAIL_ADDRESS_POSTCODE, OFFICER_CARD_ADDRESS_POSTCODE.SelectedValue)
	End Sub

	Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSave.Click
		'################################################
		'Validation
        '################################################
        If Not CheckSponsor() Then
            Exit Sub
        End If

		If Not CheckUpline() Then
			Exit Sub
		End If

		If Not CheckSender() Then
			Exit Sub
		End If
		'################################################
		

		Dim registerInfo As Entity.OfficerInfoItem
		If GlobalApp.DataSourceEditOfficer IsNot Nothing Then
			registerInfo = GlobalApp.DataSourceEditOfficer
		Else
			registerInfo = New Entity.OfficerInfoItem
		End If

		With registerInfo
			'############################### Application 1 ##############################################
			.officer_app_id = Me.OFFICER_APP_ID.Text.Replace("-", "")
			.offcier_app_tel = Me.OFFCIER_APP_TEL.Text
			.officer_firstname_tha = Me.OFFICER_FIRSTNAME_THA.Text
			.officer_lastname_tha = Me.OFFICER_LASTNAME_THA.Text
			.officer_firstname_eng = Me.OFFICER_FIRSTNAME_ENG.Text
			.officer_lastname_eng = Me.OFFICER_LASTNAME_ENG.Text
            .officer_sex = Me.OFFICER_SEX.SelectedValue
            .officer_card_type = Me.ddlCardType.SelectedValue
            If Me.ddlCardType.SelectedValue <> "OTH" Then OFFICER_CARD_TYPE_DESC.Text = ""
            .officer_card_type_desc = Me.OFFICER_CARD_TYPE_DESC.Text.Trim
			.officer_card_no = Me.OFFICER_CARD_NO.Text
			'.officer_birthdate = New Date(Me.OFFICER_BIRTHDATE.Year, Me.OFFICER_BIRTHDATE.Month, Me.OFFICER_BIRTHDATE.Day).AddYears(-543)
			If Common.UtilityDateTime.ConvertDate(Me.OFFICER_BIRTHDATE.Text).HasValue Then
				.officer_birthdate = Common.UtilityDateTime.ConvertDate(Me.OFFICER_BIRTHDATE.Text).Value.AddYears(-543)
			Else
				RaiseEvent Alert("กรุณาระบุวันเกิดตามบัตรให้ถูกรูปแบบ (วว/ดด/ปปปป)")
				Exit Sub
			End If

			Try
				If Common.UtilityDateTime.CalAge(.officer_birthdate) < 18 Then
					RaiseEvent Alert("คุณสมบัติของผู้สมัครต้องมีอายุมากกว่า 18 ปี")
					Exit Sub
				End If
			Catch ex As Exception
				Common.LogError.ReadException(ex)
			End Try
			

			'Try
			'	.officer_card_expiredate = New Date(Me.OFFICER_CARD_EXPIREDATE.Year, Me.OFFICER_CARD_EXPIREDATE.Month, Me.OFFICER_CARD_EXPIREDATE.Day).AddYears(-543)
			'Catch
			'End Try

			Try
				'.officer_card_expiredate = New Date(Me.OFFICER_CARD_EXPIREDATE.Year, Me.OFFICER_CARD_EXPIREDATE.Month, Me.OFFICER_CARD_EXPIREDATE.Day).AddYears(-543)
				If Not String.IsNullOrEmpty(Me.OFFICER_CARD_EXPIREDATE.Text) Then
					If Common.UtilityDateTime.ConvertDate(Me.OFFICER_CARD_EXPIREDATE.Text).HasValue Then
						.officer_card_expiredate = Common.UtilityDateTime.ConvertDate(Me.OFFICER_CARD_EXPIREDATE.Text).Value.AddYears(-543)
					Else
						RaiseEvent Alert("กรุณาระบุวันบัตรหมดอายุให้ถูกรูปแบบ (วว/ดด/ปปปป)")
						Exit Sub
					End If
				End If
			Catch
			End Try

			.officer_card_expiredate_type = Me.OFFICER_CARD_EXPIREDATE_TYPE.SelectedValue
			.officer_married_status = Me.OFFICER_MARRIED_STATUS.SelectedValue


			Select Case Me.hdfStatusID.Value.ToUpper
				Case "WT"
					If Me.chbChangeStatus.Checked Then
						.officer_status = "WN"
					Else
						.officer_status = Nothing
					End If
				Case "WC"
					If Me.chbChangeStatus.Checked Then
						.officer_status = "WN"
					Else
						.officer_status = Nothing
					End If
				Case "WN"
					If Me.chbChangeStatus.Checked Then
						.officer_status = "RENEW"
					ElseIf Me.chbChangeStatus1.Checked Then
						.officer_status = "WC"
					ElseIf Me.chbChangeStatus2.Checked Then
						.officer_status = "CE"
					Else
						.officer_status = Nothing
					End If
				Case "CE"
					If Me.chbChangeStatus.Checked Then
						.officer_status = "WN"
					Else
						.officer_status = Nothing
					End If
				Case "EP"
					If Me.chbChangeStatus.Checked Then
						.officer_status = "RENEW"
					Else
						.officer_status = Nothing
					End If
				Case Else
					If Me.chbChangeStatus.Checked Then
						.officer_status = "CE"
					ElseIf Me.chbChangeStatus1.Checked Then
						.officer_status = "WC"
					Else
						.officer_status = Nothing
					End If
			End Select
			'If Me.chbChangeStatus.Checked Then
			'	.officer_status = "WN"
			'Else
			'	.officer_status = Nothing
			'End If


			.officer_remark = Me.OFFICER_REMARK.Text
			If GlobalApp.Authen IsNot Nothing Then .upd_user_id = GlobalApp.Authen.offcier_id
			'############################### Validation ##############################################

			If Common.UtilityDateTime.CalAge(registerInfo.officer_birthdate) < 18 Then
				RaiseEvent Alert("คุณสมบัติของผู้สมัครต้องมีอายุมากกว่า 18 ปี")
				Exit Sub
			End If

			If registerInfo.officer_birthdate >= DateTime.Today Then
				RaiseEvent Alert("ท่านต้องเลือกวันเกิดให้มีค่าน้อยกว่าวันที่ปัจจุบัน")
				Exit Sub
			End If

			If Not String.IsNullOrEmpty(registerInfo.offcier_app_tel) Then
				If BL.Officer.CheckDupAppTel(registerInfo.offcier_app_tel, registerInfo.officer_id) Then
					RaiseEvent Alert("ไม่สามารถใช้หมายเลขโทรศัพท์ " & registerInfo.offcier_app_tel & " ในการสมัครได้ เพราะพบหมายเลขโทรศัพท์นี้ในระบบแล้ว")
					Exit Sub
				End If
			End If

			If Not String.IsNullOrEmpty(registerInfo.officer_card_no) Then
				If BL.Officer.CheckDupCardNo(registerInfo.officer_card_no, registerInfo.officer_id) Then
					RaiseEvent Alert("พบหมายเลขบัตรประชาชน " & registerInfo.officer_card_no & " นี้ในการสมัครเป็นนักธุรกิจแล้ว")
					Exit Sub
				End If
			End If
			'############################### Application 1 ##############################################

			'############################### Application 2 ##############################################
			.officer_card_address_no = Me.OFFICER_CARD_ADDRESS_NO.Text
			.officer_card_address_moo = Me.OFFICER_CARD_ADDRESS_MOO.Text
			.officer_card_address_building = Me.OFFICER_CARD_ADDRESS_BUILDING.Text
			.officer_card_address_soi = Me.OFFICER_CARD_ADDRESS_SOI.Text
			.officer_card_address_street = Me.OFFICER_CARD_ADDRESS_STREET.Text
			.officer_card_address_province = Me.OFFICER_CARD_ADDRESS_PROVINCE.SelectedItem.Text
			.officer_card_address_district = Me.OFFICER_CARD_ADDRESS_DISTRICT.SelectedItem.Text
			.officer_card_address_sub = Me.officer_card_address_subICT.SelectedItem.Text
			.officer_card_address_postcode = Me.OFFICER_CARD_ADDRESS_POSTCODE.SelectedItem.Text

			.officer_mail_address_no = Me.OFFICER_MAIL_ADDRESS_NO.Text
			.officer_mail_address_moo = Me.OFFICER_MAIL_ADDRESS_MOO.Text
			.officer_mail_address_building = Me.OFFICER_MAIL_ADDRESS_BUILDING.Text
			.officer_mail_address_soi = Me.OFFICER_MAIL_ADDRESS_SOI.Text
			.officer_mail_address_street = Me.OFFICER_MAIL_ADDRESS_STREET.Text
			.officer_mail_address_province = Me.OFFICER_MAIL_ADDRESS_PROVINCE.SelectedItem.Text
			.officer_mail_address_district = Me.OFFICER_MAIL_ADDRESS_DISTRICT.SelectedItem.Text
			.officer_mail_address_sub = Me.OFFICER_MAIL_ADDRESS_SUB_DISTRICT.SelectedItem.Text
			.officer_mail_address_postcode = Me.OFFICER_MAIL_ADDRESS_POSTCODE.SelectedItem.Text
			.officer_mail_phone = Me.OFFICER_MAIL_PHONE.Text
			.officer_mail_email = Me.OFFICER_MAIL_EMAIL.Text
            .upd_date = Now.AddYears(-543)
			.upd_user_id = GlobalApp.Authen.offcier_id
			'############################### Application 2 ##############################################

			'############################### Application 3 ##############################################
			If Not String.IsNullOrEmpty(Me.OFFICER_SPONSOR.Text) Then
				.officer_sponsor = Me.hfSponserID.Value
				.officer_sponsor_appid = Me.OFFICER_SPONSOR.Text
				.officer_sponsor_name = Me.OFFICER_SPONSOR_NAME.Text
				.officer_sponsor_surename = Me.OFFICER_SPONSOR_SURENAME.Text
				.officer_sponsor_tel = Me.OFFICER_SPONSOR_TEL.Text
			End If

			If Not String.IsNullOrEmpty(Me.OFFICER_UPLINE.Text) Then
				.officer_upline = Me.hfUplineID.Value
				.officer_upline_appid = Me.OFFICER_UPLINE.Text
				.officer_upline_name = Me.OFFICER_UPLINE_NAME.Text
				.officer_upline_surename = Me.OFFICER_UPLINE_SURENAME.Text
				.officer_upline_tel = Me.OFFICER_UPLINE_TEL.Text
				If OFFICER_UPLINE_EMPTYSIDE.Text.Equals("ซ้าย") Then
					.officer_upline_emptyside = "L"
					.officer_upline_side = "L"
				Else
					.officer_upline_emptyside = "R"
					.officer_upline_side = "R"
				End If
			End If

			If Not String.IsNullOrEmpty(Me.OFFICER_SENDER.Text) Then
				.officer_sender = Me.hfSenderID.Value
				.officer_sender_appid = Me.OFFICER_SENDER.Text
				.officer_sender_name = Me.OFFICER_SENDER_NAME.Text
				.officer_sender_surename = Me.OFFICER_SENDER_SURENAME.Text
				.officer_sender_tel = Me.OFFICER_SENDER_TEL.Text
			End If

			.officer_downline_left = String.Empty
			.officer_downline_right = String.Empty
			'############################### Application 3 ##############################################

			'############################### Application 4 ##############################################
			.officer_bank_account_name = Me.OFFICER_BANK_ACCOUNT_NAME.Text
			.bank_id = Me.BANK_ID.SelectedValue
			.officer_bank_branch = Me.OFFICER_BANK_BRANCH.Text
			.officer_bank_account_code = Me.OFFICER_BANK_ACCOUNT_CODE.Text
			If Me.BANK_ID.SelectedValue.Equals("0") Then
				.bank_name = String.Empty
			Else
				.bank_name = Me.BANK_ID.SelectedItem.Text
			End If
			'############################### Application 4 ##############################################

		End With

		'change password
		Dim pwd As Entity.UserInfoItem = Nothing
		If Not String.IsNullOrEmpty(Me.txtPassword.Text) Then
			pwd = New Entity.UserInfoItem
			pwd.user_id = registerInfo.officer_app_id
			pwd.upd_user_id = GlobalApp.Authen.offcier_id
			pwd.user_password = Me.txtPassword.Text
			pwd.offcier_id = registerInfo.officer_id
            pwd.upd_date = Now.AddYears(-543)
		End If

		Try
			If GlobalApp.DataSourceEditOfficer IsNot Nothing Then
                If BL.Officer.UpdateOfficer(registerInfo, pwd) Then
                    GlobalApp.DepositMessage = "แก้ไขใบสมัครเรียบร้อยแล้ว"
                    If HttpContext.Current.User.IsInRole("admin") Then
                        Response.Redirect("OfficerManagement.aspx", False)
                    Else
                        Response.Redirect("ViewStatusRegister.aspx", False)
                    End If
                Else
                    RaiseEvent Alert("ไม่สามารถบันทึกใบสมัครได้")
                End If
			Else
				RaiseEvent Alert("ข้อมูลใบสมัครไม่สมบรูณ์ กรุณาทำการกรอกข้อมูลใบสมัครอีกครั้ง")
			End If
		Catch ex As Exception
			RaiseEvent Alert("ระบบไม่สามารถทำการบันทึกใบสมัครได้ กรุณาติดต่อเจ้าหน้า" & Environment.NewLine & Common.LogError.ReadException(ex))
		End Try
	End Sub

	Protected Sub gvOffical_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvOffical.PageIndexChanging
		Me.gvOffical.DataSource = Me.Session("sponsorsearch")
		Me.gvOffical.PageIndex = e.NewPageIndex
		Me.gvOffical.DataBind()
		Me.mpu.Show()
	End Sub

	Protected Sub gvOfficalUpline_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvOfficalUpline.PageIndexChanging
		Me.gvOfficalUpline.DataSource = Me.Session("uplinesearch")
		Me.gvOfficalUpline.PageIndex = e.NewPageIndex
		Me.gvOfficalUpline.DataBind()
		Me.mpuUpline.Show()
	End Sub

	Protected Sub gvOfficalSender_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvOfficalSender.PageIndexChanging
		Me.gvOfficalSender.DataSource = Me.Session("sendersearch")
		Me.gvOfficalSender.PageIndex = e.NewPageIndex
		Me.gvOfficalSender.DataBind()
		Me.mpuSender.Show()
	End Sub

	Protected Sub imgUPLINEPopup_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgUPLINEPopup.Click
		Me.mpuUpline.Show()
		btnUplineSearch_Click(Nothing, Nothing)
	End Sub

	Protected Sub imgSENDERPopup_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgSENDERPopup.Click
		mpuSender.Show()
		btnSenderSearch_Click(Nothing, Nothing)
	End Sub

	Protected Sub imgSponsorPopup_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgSponsorPopup.Click
		mpu.Show()
		btnSponsorSearch_Click(Nothing, Nothing)
	End Sub

	Private Function CheckSponsor() As Boolean
		If Not String.IsNullOrEmpty(Me.OFFICER_SPONSOR.Text) Then
			Try
				Dim officer As Entity.OfficerInfoItem
				If HttpContext.Current.User.IsInRole("admin") Then
					officer = BL.Officer.LoadSponser_Staff(Me.OFFICER_SPONSOR.Text)
				Else
					officer = BL.Officer.LoadSponser_Member(Me.OFFICER_SPONSOR.Text, GlobalApp.Authen.offcier_id)
				End If

				If officer IsNot Nothing Then
					OFFICER_SPONSOR.Text = officer.officer_app_id
					OFFICER_SPONSOR_NAME.Text = officer.officer_firstname_tha
					OFFICER_SPONSOR_SURENAME.Text = officer.officer_lastname_tha
					OFFICER_SPONSOR_TEL.Text = officer.offcier_app_tel
					Me.hfSponserID.Value = officer.officer_id
					Return True
				Else
					OFFICER_SPONSOR_NAME.Text = String.Empty
					OFFICER_SPONSOR_SURENAME.Text = String.Empty
					OFFICER_SPONSOR_TEL.Text = String.Empty
					Me.hfSponserID.Value = String.Empty
					RaiseEvent Alert("ไม่พบข้อมูลสปอนเซอร์")
					Return False
				End If
			Catch ex As Exception
				Common.LogError.AddLogException(ex)
				RaiseEvent Alert("พบข้อผิดพลาดในการค้นหาข้อมูลสปอนเซอร์")
				Return False
			End Try
		Else
			Return True
		End If
	End Function

	Private Function CheckUpline() As Boolean
		If String.IsNullOrEmpty(Me.OFFICER_UPLINE.Text) Then
			RaiseEvent Alert("กรุณาระบุข้อมูลอัพไลน์")
			Return False
		Else
			Return True
		End If
	End Function

	Private Function CheckSender() As Boolean
		If Not String.IsNullOrEmpty(Me.OFFICER_SENDER.Text) Then
			Try
				Dim officer As Entity.OfficerInfoItem
				officer = BL.Officer.LoadSender_Member(Me.OFFICER_SENDER.Text, Me.OFFICER_SPONSOR.Text)

				If officer IsNot Nothing Then
					OFFICER_SENDER.Text = officer.officer_app_id
					OFFICER_SENDER_NAME.Text = officer.officer_firstname_tha
					OFFICER_SENDER_SURENAME.Text = officer.officer_lastname_tha
					OFFICER_SENDER_TEL.Text = officer.offcier_app_tel
					Me.hfSenderID.Value = officer.officer_id
					Return True
				Else
					OFFICER_SENDER_NAME.Text = String.Empty
					OFFICER_SENDER_SURENAME.Text = String.Empty
					OFFICER_SENDER_TEL.Text = String.Empty
					Me.hfSenderID.Value = String.Empty
					RaiseEvent Alert("ไม่พบข้อมูลผู้ยื่นใบสมัคร")
					Return False
				End If
			Catch ex As Exception
				Common.LogError.AddLogException(ex)
				RaiseEvent Alert("พบข้อผิดพลาดในการค้นหาข้อมูลผู้ยื่นใบสมัคร")
				Return False
			End Try
		Else
			Return True
		End If
	End Function

	Protected Sub btnCheckSponsor_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCheckSponsor.Click
		CheckSponsor()
	End Sub

	Protected Sub btnCheckSender_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCheckSender.Click
		CheckSender()
	End Sub

	Public Sub RemoveSession()
		Me.Session.Remove("sponsorsearch")
		Me.Session.Remove("uplinesearch")
		Me.Session.Remove("sendersearch")
	End Sub

	Protected Sub OFFICER_CARD_ADDRESS_PROVINCE_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles OFFICER_CARD_ADDRESS_PROVINCE.SelectedIndexChanged
		Me.LoadDistinct1()
		Me.LoadSubDistinct1()
		Me.LoadPostCode1()
	End Sub

	Protected Sub OFFICER_CARD_ADDRESS_DISTRICT_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles OFFICER_CARD_ADDRESS_DISTRICT.SelectedIndexChanged
		Me.LoadSubDistinct1()
		Me.LoadPostCode1()
	End Sub

	Protected Sub officer_card_address_subICT_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles officer_card_address_subICT.SelectedIndexChanged
		Me.LoadPostCode1()
	End Sub

	Protected Sub OFFICER_MAIL_ADDRESS_PROVINCE_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles OFFICER_MAIL_ADDRESS_PROVINCE.SelectedIndexChanged
		Me.LoadDistinct2()
		Me.LoadSubDistinct2()
		Me.LoadPostCode2()
	End Sub

	Protected Sub OFFICER_MAIL_ADDRESS_DISTRICT_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles OFFICER_MAIL_ADDRESS_DISTRICT.SelectedIndexChanged
		Me.LoadSubDistinct2()
		Me.LoadPostCode2()
	End Sub

	Protected Sub OFFICER_MAIL_ADDRESS_SUB_DISTRICT_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles OFFICER_MAIL_ADDRESS_SUB_DISTRICT.SelectedIndexChanged
		Me.LoadPostCode2()
	End Sub

    Protected Sub ddlCardType_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddlCardType.SelectedIndexChanged
        Try
            If ddlCardType.SelectedValue = "OTH" Then
                lblCardType.Text = "เลขที่บัตรอื่นๆ *:"
                OFFICER_CARD_TYPE_DESC.Enabled = True
            Else
                OFFICER_CARD_TYPE_DESC.Text = ""
                If ddlCardType.SelectedValue = "PID" Then lblCardType.Text = "เลขที่บัตรประชาชน *:"
                If ddlCardType.SelectedValue = "CID" Then lblCardType.Text = "เลขที่บัตรนิติบุคคล *:"
                If ddlCardType.SelectedValue = "FID" Then lblCardType.Text = "เลขที่บัตรคนต่างชาติ *:"
                OFFICER_CARD_TYPE_DESC.Text = ""
                OFFICER_CARD_TYPE_DESC.Enabled = False
            End If
        Catch ex As Exception

        End Try
    End Sub

End Class